package net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

import repository.repo.FriendRepo;
import repository.repo.UserRepo;

/**
 * 服务器管理客户端线程
 */
public class Client extends Thread {
    private Socket socket;
    private UserRepo userRepo = new UserRepo();
    private FriendRepo firendRepo = new FriendRepo();
    public HashMap<String, Client> pool = new HashMap<>();

    public Client(Socket socket) {
        this.socket = socket;
    }

    // 持续接收客户端消息
    public void receiveMessage() {
        while (true) {
            try {
                InputStream in = socket.getInputStream();
                byte[] buffer = new byte[1024];

                int count = in.read(buffer);
                String receiveString = new String(buffer, 0, count);
                System.out.println(receiveString);

                // 对收到的内容进行解析 label:nickname:content
                String[] receiveList = receiveString.split(":");
                String label = receiveList[0];
                String nickname = receiveList[1];
                String content = receiveList[2];

                switch (label) {
                    case "register":
                        this.notice(userRepo.register(nickname, content));
                        break;
                    case "login":
                        this.notice(userRepo.login(nickname, content));
                        break;
                    case "addFriend":
                        this.notice(firendRepo.addFriendIfExist(nickname, content));
                        break;
                    case "message":
                        this.bulkMessage(nickname, content);
                        break;
                    default:
                        break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void notice(Boolean isSuccess) {
        try {
            OutputStream out = this.socket.getOutputStream();
            if (isSuccess) {
                out.write("success\n".getBytes("UTF-8"));
            } else {
                out.write("fail\n".getBytes("UTF-8"));
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 给朋友群发消息
    public void bulkMessage(String nickname, String content) {
        ArrayList<String> list = firendRepo.getFriedList(nickname);

        for (String friendNickname : list) {
            Client client = pool.get(friendNickname);
            try {
                OutputStream out = client.socket.getOutputStream();
                StringBuilder messageBuilder = new StringBuilder();
                messageBuilder.append("message:").append(nickname).append(":").append(content);
                out.write(messageBuilder.toString().getBytes("UTF-8"));
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
